package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.PayPwdActivity;
import com.alm.token.ui.data.bean.EggRankingData;
import com.alm.token.ui.data.bean.EggWalletLogData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.SuperColorEggData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.SuperColorEggView;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class EggWalletLog2PageFrag extends BaseViewPageFragment implements SuperColorEggView<SuperColorEggData> {

    public static final  int INTENT_EGGLOG_MYEGGTOKEN=2;
    public static final  int INTENT_EGGLOG_PARTANERTOKEN=3;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    Unbinder unbinder;

    private int limit = 100, page = 1;
    private boolean isfull = false;
    private int intent_type = 1;
    private String moneyType="1";

    private RefreshLoadMoreManager<EggWalletLogData.DataBeanX.DataBean> manager;

    private List<EggWalletLogData.DataBeanX.DataBean> dataBeans = new ArrayList<>();


    public static EggWalletLog2PageFrag newInstance(int intent_egglog_type ) {
        EggWalletLog2PageFrag fragment = new EggWalletLog2PageFrag();
        Bundle args = new Bundle();
        args.putInt("INTENT_EGGLOG_TYPE",intent_egglog_type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_message_base;
    }

    @Override
    protected void initVariables() {
        intent_type=getArguments().getInt("INTENT_EGGLOG_TYPE");
        moneyType=intent_type==INTENT_EGGLOG_MYEGGTOKEN?"1":"2";

    }

    @Override
    protected void initViews(View view) {
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private void initManager() {
        MessageAdapter adapter = new MessageAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        if (dataBeans != null && dataBeans.size() > position) {
//                            Intent intent = new Intent(getContext(), QuantitativeOrderDetailActivity.class);
//                            intent.putExtra("databean", dataBeans.get(position));
//                            startActivity(intent);
//                        }

                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {

                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        showWaitDialog();
                        preenter.getEggExcitationData(String.valueOf(limit), String.valueOf(page),moneyType);
                    }
                })

                .build();
    }

    @Override
    protected void loadData() {
        limit = 100;
        page = 1;
        preenter.getEggExcitationData(String.valueOf(limit), String.valueOf(page),moneyType);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    private List<EggWalletLogData.DataBeanX.DataBean> dataBeansOld = new ArrayList<>();

    @Override
    public void getData(SuperColorEggData superColorEggData) {

    }

    @Override
    public void getEggAddData(RegisterData registerData) {

    }

    @Override
    public void getEggRankingData(EggRankingData eggRankingData) {

    }

    @Override
    public void getEggWalletLogData(EggWalletLogData eggWalletLogData) {
        if (eggWalletLogData.getStatus() == 0 && eggWalletLogData.getData() != null) {


            if (page == 1) {
                dataBeans.clear();
            } else if (!isfull && dataBeans.size() > (page - 1) * 100) {
                dataBeans.removeAll(dataBeansOld);
            }

            if (eggWalletLogData.getData().getData().size() == 100) {
                isfull = true;
                page = page + 1;
            } else {
                isfull = false;
            }

            dataBeansOld = eggWalletLogData.getData().getData();
            for (int i = 0; i < dataBeansOld.size(); i++) {
                dataBeansOld.get(i).setLayoutId(MessageAdapter.LAYOUT_ID);
            }
            dataBeans.addAll(dataBeansOld);

            manager.setData(dataBeans);
        } else showToast(eggWalletLogData.getMsg());
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }


    public class MessageAdapter extends BaseAdapter<EggWalletLogData.DataBeanX.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_transformlist_base;

        public MessageAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, EggWalletLogData.DataBeanX.DataBean item, int itemType) {
            /**
             * id : 1
             * userId : 23252
             * money : 0.5
             * moneyType : 1
             * channelType : 1
             * createTime : 1557376613000
             * updateTime : null
             * beforeMoney : 1.5
             * afterMoney : 2
             * channelTypeStr : 彩蛋
             * moneyTypeStr : sETH
             */

            helper.setVisible(R.id.tv_phone0, false);
            helper.setText(R.id.tv_name0, item.getChannelTypeStr());
            helper.setText(R.id.tv_staue, CommonUtil.saveToPoint4WithNo(item.getMoney())+" "+item.getMoneyTypeStr());
            helper.setText(R.id.tv_time0, "" + CommonUtil.getDateTimeFromMillisecond(item.getCreateTime()));
        }

    }
}
